package com.sumainfor.wx.system.maket.service;

import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.wx.system.maket.dao.RdHousingExcMapper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 *
 * <p>
 *
 * @author: zc
 * @create: 2019-08-14 15:35
 **/
@Service
public class RdHousingExcService implements Serializable {

    private ReentrantLock lock = new ReentrantLock(false);

    Logger log= LoggerFactory.getLogger(RdHousingExcService.class);

    @Autowired
    RdHousingExcMapper rdHousingExcMapper;


    /**
     *  查询自由或者共有换房申请
     * @author zhoucong
     * @data 2019/8/14
     * @Param
     * @return
     */
    public List<Map<String,Object>> getHouShelfIdList(Map<String,Object> params){

        List<Map<String,Object>>result= rdHousingExcMapper.getHouShelfIdList(params);
        if(result.size()>0){
            //循环获取 换房原因
            for(Map<String,Object> map:result){
                //获取换房原因
                String ExcType= MapUtils.getString(map,"ExcType");
                //不为空
                if(StringUtils.isNotEmpty(ExcType)){
                    //截取
                    String[]ExcString=ExcType.split(",");
                    List<Map<String,Object>> ExcList=new ArrayList<Map<String,Object>>();
                    if(ExcString.length>0){
                        Map<String,Object>extMap;
                        for (String str:ExcString){
                            extMap=new HashMap<String,Object>();
                            extMap.put("ExcTypeId",str);
                            String ExcTypeName;
                            switch (str){
                                case "0":
                                    ExcTypeName="工作异动";
                                    break;
                                case "1":
                                    ExcTypeName="邻里不和";
                                    break;
                                case "2":
                                    ExcTypeName="环境不佳";
                                    break;
                                default:
                                    ExcTypeName="其他原因";
                                    break;
                            }
                            extMap.put("ExcTypeName",ExcTypeName);
                            ExcList.add(extMap);
                        }
                    }
                    map.put("ExcList",ExcList);
                }
            }
        }
        return  result;
    }


    /**
     * 查询私有或者公共换房申请池的数量
     * @author zhoucong
     * @data 2019/8/14
     * @Param
     * @return
     */
    public Integer getHouShelfIdListCount(Map<String,Object> params){
        return rdHousingExcMapper.getHouShelfIdListCount(params);
    }


    /**
     * 租客取消换房申请
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult upHousingExc(Map<String,Object> params){
        JsonResult result=new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try{
            try {
                System.out.println("换房:"+params);
                if(params.get("ExcId")==null){
                    return  result.putFailed("参数不完整");
                }
                params.put("ExcState",2);//换房申请状态（0 申请换房中 1已抢单 2租客取消）
                params.put("by", "auto");//操作人
                params.put("term", "1");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                params.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(params);
                Integer count=rdHousingExcMapper.upHousingExc(params);
                if (count>0){
                    result.putSuccess("操作成功");
                }else{
                    result.putFailed("服务器繁忙");
                }
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                result.putFailed("服务器繁忙");
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }






}
